<template>
	<view class="hehuoren">
		<u-sticky>
			<view style="background-color: #fff">
				<view class="head">
					<view class="flex align-center justify-between">
						<view class="flex align-center">
							<view style="font-size: 0">
								<image class="avatar" :src="user.avatar"></image>
							</view>
							<view class="flex flex-column justify-between">
								<view class="name">
									{{ user.name }}
								</view>
								<view style="font-size: 0">
									<image class="label" src="@/pages/news/static/1.png"></image>
								</view>
							</view>
						</view>
						<view @click="goPage('/pages/schedule/joinStore')" class="flex align-center justify-center btn">成为更多店铺合伙人>></view>
					</view>
				</view>
				<view class="tab">
					<view :class="['tab-item', { active: tabIndex == 1 }]" @click="toggle(1)">合作店铺</view>
					<view :class="['tab-item', { active: tabIndex == 2 }]" @click="toggle(2)">他人邀请记录</view>
					<view :class="['tab-item', { active: tabIndex == 3 }]" @click="toggle(3)">自主申请记录</view>
				</view>
				<view style="height: 20rpx; background: #f4f5f9"></view>
			</view>
		</u-sticky>
		<block v-if="tabIndex == 1">
			<view v-for="(item, index) in list" :key="index" class="flex align-center justify-between list">
				<view class="flex-1 flex align-center list-item">
					<view class="pic">
						<image :src="item.avatar_url"></image>
					</view>
					<view class="name">
						{{ item.shopname }}
					</view>
				</view>
				<view @click="out(item)" style="font-size: 28rpx; color: #aaa">退出</view>
			</view>
		</block>
		<block v-else>
			<block v-if="list.length">
				<view class="listbox">
					<view v-for="(item, index) in list" :key="index" class="list-item">
						<view class="head">
							<view class="left">
								<view class="avatar">
									<image :src="item.shopinfo.avatar_url"></image>
								</view>
								<view class="userInfo">
									<view class="name">{{ item.shopinfo.shopname }}</view>
									<view class="mobile" v-if="tabIndex == 3">自主申请</view>
									<view class="mobile" v-else>{{ item.puserinfo.name || item.puserinfo.nickname }}邀请</view>
								</view>
							</view>
							<view class="right">
								<view :class="['status', { yellow: [3].includes(item.status), red: [4, 5].includes(item.status) }]">{{ item.status_text }}</view>
							</view>
						</view>
						<view class="msg">{{ tabIndex == 3 ? '申请' : '邀请' }}时间：{{ get_datesForDian(item.createtime) }}</view>
						<view class="msg" v-if="[1, 2].includes(item.type)">
							<view>审核时间：{{ get_datesForDian(item.audit_time) }}</view>
							<view>审核人：{{ item.audituserinfo.name || item.audituserinfo.nickname }}</view>
						</view>
					</view>
				</view>
			</block>
		</block>
		<view class="empty" v-if="!list.length">暂无数据</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			user: {},
			tabIndex: 1,
			list: [],
			page: 1,
			totalPage: 1
		};
	},
	onLoad(option) {
		let that = this;
		this.$request({
			url: 'member/getInfo',
			method: 'POST'
		}).then((res) => {
			if (res.data.code == 1) {
				that.user = res.data.data;
			} else {
				uni.showToast({
					title: res.data.msg,
					icon: 'error'
				});
			}
		});
		if (option?.index) {
			this.tabIndex = option?.index;
			this.getDataList();
		} else {
			this.getList();
		}
	},
	onReachBottom() {
		this.page++;
		if (this.page > this.totalPage) return this.$wanlshop.msg('我也是有底线的');
		if (this.tabIndex == 1) {
			this.getList();
		} else {
			this.getDataList();
		}
	},
	methods: {
		toggle(n) {
			this.tabIndex = n;
			this.list = [];
			if (n == 1) {
				this.getList();
			} else if (n == 2) {
				this.getDataList();
			} else if (n == 3) {
				this.getDataList();
			}
		},
		out(item) {
			uni.showModal({
				title: '确认退出吗?',
				success: (res) => {
					if (res.confirm) {
						this.$request({
							url: 'Partners/shop_partner_del',
							method: 'POST',
							data: {
								shop_id: item.id
							}
						}).then((res) => {
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							});
							if (res.data.code == 1) {
								this.getList();
							}
						});
					}
				}
			});
		},
		getList() {
			uni.showLoading({ title: '数据请求中···' });
			this.$request({
				url: 'Partners/shop_partner_list',
				method: 'POST'
			}).then((res) => {
				const { code, data, msg } = res.data;
				if (code == 1) {
					if (this.page == 1) {
						this.list = data;
					} else {
						this.list = [...this.list, ...data];
					}
				} else {
					uni.showToast({
						title: msg,
						icon: 'error'
					});
				}
				uni.hideLoading();
			});
		},
		getDataList() {
			uni.showLoading({ title: '数据请求中···' });
			this.$request({
				url: 'Partners/shop_partner_invite_log',
				method: 'POST',
				data: {
					type: this.tabIndex
				}
			}).then((res) => {
				const { code, data, msg } = res.data;
				if (code == 1) {
					this.totalPage = data.last_page;
					this.list = [...this.list, ...data.data];
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'error'
					});
				}
				uni.hideLoading();
			});
		},
		goPage(e) {
			uni.navigateTo({
				url: e
			});
		}
	}
};
</script>

<style scoped lang="scss">
.hehuoren {
	.head {
		padding-top: 70rpx;
		padding-left: 32rpx;
		padding-right: 32rpx;
		.avatar {
			width: 84rpx;
			height: 84rpx;
			border-radius: 50%;
			margin-right: 32rpx;
		}
		.name {
			font-size: 30rpx;
			color: #000;
		}
		.label {
			width: 132rpx;
			height: 32rpx;
		}
		.btn {
			font-size: 22rpx;
			color: #fff;
			width: 274rpx;
			height: 70rpx;
			background: #087cd4;
			border-radius: 35rpx;
		}
	}
	.list {
		padding: 24rpx 32rpx;
		border-bottom: 2rpx solid #efefef;
		.pic {
			font-size: 0;
			margin-right: 20rpx;
			image {
				width: 64rpx;
				height: 64rpx;
				border-radius: 50%;
			}
		}
		.name {
			font-size: 30rpx;
			font-weight: 500;
			color: #333;
		}
	}
	.listbox {
		padding: 0 32rpx 80rpx;
		.list-item {
			padding: 24rpx 0;
			border-bottom: 2rpx solid #efefef;
		}
		.head {
			display: flex;
			justify-content: space-between;
			padding: 0;
			.left {
				display: flex;
				align-items: center;
				flex: 1;
			}
			.avatar {
				width: 84rpx;
				height: 84rpx;
				image {
					width: 100%;
					height: 100%;
					border-radius: 50%;
				}
			}
			.userInfo {
				padding-left: 20rpx;
				.name {
					padding-bottom: 8rpx;
					line-height: 42rpx;
					font-size: 30rpx;
					color: #333;
				}
				.mobile {
					line-height: 34rpx;
					font-size: 24rpx;
					color: #666;
				}
			}
			.right {
				margin-left: 40rpx;
				text-align: right;
			}
			.status {
				padding: 4rpx 0 12rpx;
				line-height: 36rpx;
				font-size: 26rpx;
				color: #999;
			}
			.yellow {
				color: #ebbe87;
			}
			.red {
				color: #ff3f4a;
			}
			.shopname {
				line-height: 34rpx;
				font-size: 24rpx;
				color: #333;
			}
		}
		.msg {
			display: flex;
			justify-content: space-between;
			margin-top: 8rpx;
			line-height: 36rpx;
			font-size: 24rpx;
			color: #888;
		}
	}
	.empty {
		padding-top: 80rpx;
		text-align: center;
		color: #666;
		font-size: 28rpx;
	}
}
.tab {
	display: flex;
	margin-top: 60rpx;
	padding: 0 32rpx;
	.tab-item {
		position: relative;
		height: 70rpx;
		font-size: 30rpx;
		color: #666;
		z-index: 1;
		& + .tab-item {
			margin-left: 60rpx;
		}
	}
	.active {
		color: #333;
		font-weight: bold;
		&::after {
			position: absolute;
			left: 50%;
			bottom: 0;
			content: '';
			width: 64rpx;
			height: 4rpx;
			background-color: #0674d5;
			transform: translate(-50%, 0);
			z-index: 2;
		}
	}
}
</style>
